package com.suse.lhw.resourceservere.controller;

import com.suse.lhw.hms.exersiseserver.pojo.out.EIAndName;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.FIAndName;
import com.suse.lhw.resourceservere.client.exersicserverClient;
import com.suse.lhw.resourceservere.client.foodserverClient;
import com.suse.lhw.resourceservere.client.personserverClient;
import com.suse.lhw.resourceservere.domin.PHealthData;
import com.suse.lhw.resourceservere.domin.PHealthrecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.resourceservere.com.suse.lhw.security.controller
 * @ClassName: PersonUserController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/5/24 11:50
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/24 11:50
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
@RequestMapping("personUser")
public class PersonUserController {
    @Autowired
    personserverClient personserverClient;
    @Autowired
    exersicserverClient exersicserverClient;
    @Autowired
    foodserverClient foodserverClient;

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @RequestMapping("/toHealtheyData")
    public String toHealtheyData(HttpServletRequest httpServletRequest, Model model) {
        /**
         * @Author:Hongwei Li
         * @Description:从数据库中得到健康信息的相关信息
         * @Date:0:53 2020/6/22
         * @Param:[httpServletRequest, model]
         * @Return:java.lang.String
         **/
        PHealthData pHealthData = personserverClient.selectAHealthData();
        model.addAttribute("pHealthData", pHealthData);
        System.out.println(pHealthData.toString());
        model.addAttribute("a", pHealthData.getAge());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("sss" + simpleDateFormat.format(pHealthData.getEndtime()));
        model.addAttribute("startdate", simpleDateFormat.format(pHealthData.getStarttime()));
        model.addAttribute("enddate", simpleDateFormat.format(pHealthData.getEndtime()));
        return "healthy-data";
    }

    @RequestMapping("/updateHealthData")
    @ResponseBody
    public String updateHealthData(HttpServletRequest httpServletRequest, Model model, @RequestBody PHealthData pHealthData) {
        System.out.println("the web  pHealthDate is" + pHealthData.toString());
        PHealthData select = personserverClient.selectAHealthData();
        System.out.println("users healthdata id is " + select.getId());
        String userId = "aaa";
        pHealthData.setId(select.getId());
        pHealthData.setUserid(userId);
        System.out.println("pHealthData is :" + pHealthData.toString());
        String result = personserverClient.updateAHealthData(pHealthData);
//        model.addAttribute("msg",result);
        return result;
    }

    @ResponseBody
    @RequestMapping("getRecord")
    public Map<String, Object> getRecord() {
        String userId = "aaa";
        Map<String, Object> map = new HashMap<String, Object>();
        int cosume = 0;
        int take = 0;
        //先找出所有的食品实例
        List<FIAndName> fin = foodserverClient.selectFIsByUserId();
        if (fin != null && !fin.isEmpty()) {
            for (FIAndName fiAndName : fin) {
                take += fiAndName.getCalories();
            }
            map.put("fin", fin);
        }
        //再找出所有的运动实例
        List<EIAndName> ein = exersicserverClient.selectMovInstance();
        if (ein != null && !ein.isEmpty()) {
            for (EIAndName eiAndName : ein) {
                cosume += eiAndName.getCalories();
            }
            map.put("ein", ein);
        }

        //找出用户的健康档案
        PHealthData pHealthData = personserverClient.selectAHealthData();
        PHealthrecord pHealthrecord = personserverClient.selectAHealthRecord(take, cosume);
        System.out.println("PHelthrecord" +
                pHealthrecord.toString());
        map.put("take", take);
        map.put("cosume", cosume);
        map.put("NewWeight", pHealthData.getNewWeight());
        map.put("TargetWeight", pHealthData.getTargetWeight());
        map.put("EatCalories", pHealthrecord.getEatCalories());
        map.put("regular", pHealthrecord.getRegular());
        return map;

    }
}
